package ftp;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.xml.crypto.Data;

/**
 * 界面布局类
 * @author jk_meng蒙秋敏
 *
 */
@SuppressWarnings("serial")
public class FTP_Panel extends javax.swing.JFrame {
	
    public javax.swing.JTabbedPane clientInfoPane;
    public javax.swing.JPanel contactPanel;
    public javax.swing.JPanel jPanel1;
    public javax.swing.JScrollPane fileScrollPane;
    public static javax.swing.JTextField status;
    public javax.swing.JPanel personalPanel;
    public static javax.swing.JButton connectButton;
    public static javax.swing.JTextField url;
    public javax.swing.JLabel serverIP_Label;
    public static javax.swing.JTextField password;
    public javax.swing.JLabel serverMima_Label;
    public static javax.swing.JTextField port;
    public javax.swing.JLabel serverPort_Label;
    public static javax.swing.JButton closeButton;
    public static javax.swing.JTextField username;
    public javax.swing.JLabel serverUser_Label;
    public javax.swing.JLabel title;
    public static  Date time;
    public static PrintWriter out;
    public FileWriter write ;

//	ftp文件进行右键操作
    public JPopupMenu popup = new JPopupMenu();;
    public JMenuItem refresh = new JMenuItem("刷新"); 	//右键弹出菜单
    public JMenuItem delete  = new JMenuItem("删除");  
    public JMenuItem download  = new JMenuItem("下载"); 
    public JMenuItem upload  = new JMenuItem("上传"); 
    public JMenuItem rename =  new JMenuItem("重命名");
    public JMenuItem upDir = new JMenuItem("上级目录");
    public JMenuItem showLog = new JMenuItem("显示日志信息");
	
	final static DefaultListModel m=new DefaultListModel(); 	//显示文件列表
	static JList fileList = new JList(m);//列表控件	
	
    /**
     * FTP_Panel构造
     */
    public FTP_Panel() {
        initPannels();   //面板各个成员初始化
        initPopup(); 		//弹出菜单
        time = new Date();
        status.setEditable(false);
        try
		{	
			write = new FileWriter("log.txt",true);
			out = new PrintWriter(write,true);
			
		}
		catch(IOException ioe)
		{
			ioe.printStackTrace();
		}
    }
    
    
    /**
     * 面板各个成员初始化
     */
    private void initPannels() {
        jPanel1 = new javax.swing.JPanel();
        title = new javax.swing.JLabel();
        clientInfoPane = new javax.swing.JTabbedPane();
        personalPanel = new javax.swing.JPanel();
        serverIP_Label = new javax.swing.JLabel();
        url = new javax.swing.JTextField();
        serverPort_Label = new javax.swing.JLabel();
        port = new javax.swing.JTextField();
        serverUser_Label = new javax.swing.JLabel();
        serverMima_Label = new javax.swing.JLabel();
        password = new javax.swing.JTextField();
        username = new javax.swing.JTextField();
        connectButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        contactPanel = new javax.swing.JPanel();
        fileScrollPane = new javax.swing.JScrollPane();
        status = new javax.swing.JTextField();
        
        this.addWindowListener(									//关闭程序时的操作
				new WindowAdapter()
				{
					public void windowClosing(WindowEvent e)
					{
						FTP_Manager.close();   //服务器关闭
						System.exit(0);
					}
				}
			);

        jPanel1.setBackground(new java.awt.Color(250, 250, 250));

        title.setFont(new java.awt.Font("Dialog", 0, 24)); // NOI18N
        title.setText("FTP客户端"); // NOI18N

        clientInfoPane.setFont(new java.awt.Font("黑体", 0, 14)); // NOI18N

        personalPanel.setBackground(new java.awt.Color(255, 255, 255));
        personalPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        serverIP_Label.setFont(new java.awt.Font("宋体", 0, 14)); // NOI18N
        serverIP_Label.setText("IP：");
        serverPort_Label.setFont(new java.awt.Font("宋体", 0, 14)); // NOI18N
        serverPort_Label.setText("端口：");
        serverUser_Label.setFont(new java.awt.Font("宋体", 0, 14)); // NOI18N
        serverUser_Label.setText("用户：");
        serverMima_Label.setFont(new java.awt.Font("宋体", 0, 14)); // NOI18N
        serverMima_Label.setText("密码：");
        
        
        connectButton.setFont(new java.awt.Font("宋体", 1, 18)); // NOI18N
        connectButton.setText("连接");
        connectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	connectButtonActionPerformed(evt);
            }
        });

        closeButton.setFont(new java.awt.Font("宋体", 1, 18)); // NOI18N
        closeButton.setText("断开");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                serverStopActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout personalPanelLayout = new javax.swing.GroupLayout(personalPanel);
        personalPanel.setLayout(personalPanelLayout);
        personalPanelLayout.setHorizontalGroup(
            personalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(personalPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(personalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(serverIP_Label)
                    .addComponent(serverPort_Label)
                    .addComponent(serverUser_Label)
                    .addComponent(serverMima_Label))
                .addGap(7, 7, 7)
                .addGroup(personalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(url, javax.swing.GroupLayout.DEFAULT_SIZE, 495, Short.MAX_VALUE)
                    .addComponent(port, javax.swing.GroupLayout.DEFAULT_SIZE, 495, Short.MAX_VALUE)
                    .addComponent(username)
                    .addComponent(password))
                .addContainerGap())
            .addGroup(personalPanelLayout.createSequentialGroup()
                .addGap(183, 183, 183)
                .addComponent(closeButton)
                .addGap(100, 100, 100)
                .addComponent(connectButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        personalPanelLayout.setVerticalGroup(
            personalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(personalPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(personalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(serverIP_Label)
                    .addComponent(url, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(personalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(serverPort_Label)
                    .addComponent(port, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(personalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(serverUser_Label)
                    .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(personalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(serverMima_Label)
                    .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 104, Short.MAX_VALUE)
                .addGroup(personalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(connectButton)
                    .addComponent(closeButton))
                .addGap(81, 81, 81))
        );

        clientInfoPane.addTab("\u8fde\u63a5\u5ba2\u6237\u7aef", personalPanel);   //添加tab面板

        contactPanel.setBackground(new java.awt.Color(255, 255, 255));
        contactPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        
        fileScrollPane = new JScrollPane(fileList,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        fileScrollPane.setViewportView(fileList);

        javax.swing.GroupLayout contactPanelLayout = new javax.swing.GroupLayout(contactPanel);
        contactPanel.setLayout(contactPanelLayout);
        contactPanelLayout.setHorizontalGroup(
            contactPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(fileScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE)
        );
        contactPanelLayout.setVerticalGroup(
            contactPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(fileScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
        );

        clientInfoPane.addTab("\u6587\u4ef6\u9009\u62e9", contactPanel);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(title)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(clientInfoPane, javax.swing.GroupLayout.PREFERRED_SIZE, 531, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(title, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(clientInfoPane, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        status.setText("状态");
        status.setFont(new java.awt.Font("黑体", 0, 14)); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 543, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(status, javax.swing.GroupLayout.PREFERRED_SIZE, 543, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(27, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(status, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        
       
        url.setText("125.217.40.");  
		port.setText("21");
		username.setText("jk_meng_ftp");
		password.setText("mmye90105");

        pack();      //页面显示
    }
    
    
	/**
	 * 右键菜单初始化
	 */
	void initPopup()  
	{
		refresh.addActionListener(new ActionListener()   //刷新右键
		{
			public void actionPerformed(ActionEvent e)
			{
				FTP_Manager.update();                   //文件列表更新
			}
		});
		popup.add(refresh);
		
		delete.addActionListener(new ActionListener()   //删除右键
		{
			public void actionPerformed(ActionEvent e)
			{
				FTP_Manager.deleteFile();              //文件列表删除选中文件
			}
		});
		popup.add(delete);
		
		download.addActionListener(new ActionListener()    //下载右键
		{
			public void actionPerformed(ActionEvent e)
			{
				FTP_Manager.downloadFile();              //下载选中文件
			} 
		});
		popup.add(download);
		
		upload.addActionListener(new ActionListener()     //下载右键
		{
			public void actionPerformed(ActionEvent e)
			{
				FTP_Manager.uploadFile();                 //上传选中文件
			}
		});
		popup.add(upload);
		
		rename.addActionListener(new ActionListener()//重命名右键
		{
			public void actionPerformed(ActionEvent e)
			{
				FTP_Manager.renameFile();  
			}
		});
		popup.add(rename);
		
		upDir.addActionListener(new ActionListener() //上一级
		{
			public void actionPerformed(ActionEvent e)
			{
				FTP_Manager.upDir();
			}
		});
		popup.add(upDir);
		
		showLog.addActionListener(new ActionListener()  //显示日志
		{
			public void actionPerformed(ActionEvent e)
			{
				FTP_Manager.ShowLog();
			}
		});
		popup.add(showLog);
		
		fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		fileList.addMouseListener(new MouseAdapter()
		{
			public void mousePressed(MouseEvent e) 
			{
				if(e.getClickCount()==2)        //鼠标双击的效果
					FTP_Manager.downDir();
				else
					maybeShowPopup(e);
			}
			
			public void mouseReleased(MouseEvent e) 
			{
				maybeShowPopup(e);
			}
			
			private void maybeShowPopup(MouseEvent e) 
			{
				if (e.isPopupTrigger()) 
				{
					popup.show(e.getComponent(), e.getX(), e.getY());//在相应的位置显示菜单组件
					
		    }
		  }
		});	
	}

    /**
     * 关闭服务器监听事件
     * @param evt
     */
    private void serverStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_serverStopActionPerformed
    	closeButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				FTP_Manager.close();  //关闭服务器方法
				time = new Date();
				out.println(time+" 离开服务器:"+url.getText());
			}
		});
    }

    /**
     * 连接服务器监听事件
     * @param evt
     */
    private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_serverConnActionPerformed
    	connectButton.addActionListener( new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				FTP_Manager.connect();   //连接方法
			}
		});
    
    
    }
 

}
